#include<iostream>
#include<mutex>
class LockGuard
{
public:
	LockGuard(std::mutex& mtx)
		:_mtx(mtx)
	{
		_mtx.lock();
	}
	~LockGuard()
	{
		_mtx.unlock();
	}
private:
	std::mutex& _mtx;
};
void Print(int n, int& x, std::mutex& mtx)
{
	LockGuard lock(mtx);
	for (int i = 0; i < n; i++)
	{
		x++;
	}
}
int main()
{
	int x = 0;
	std::mutex mtx;
	std::thread t1(Print, 10000000, std::ref(x), std::ref(mtx));
	std::thread t2(Print, 20000000, std::ref(x), std::ref(mtx));

	t1.join();
	t2.join();
	std::cout << "x val is " << x << std::endl;
	return 0;
}